package com.ruoyix.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.flowable.domain.bo.BpmTaskExtBo;
import com.ruoyix.flowable.domain.vo.BpmTaskExtVo;
import com.ruoyix.flowable.domain.BpmTaskExt;
import com.ruoyix.flowable.mapper.BpmTaskExtMapper;
import com.ruoyix.flowable.service.IBpmTaskExtService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 流程任务扩展Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@RequiredArgsConstructor
@Service
public class BpmTaskExtServiceImpl implements IBpmTaskExtService {

    private final BpmTaskExtMapper baseMapper;

    /**
     * 查询流程任务扩展
     */
    @Override
    public BpmTaskExtVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询流程任务扩展列表
     */
    @Override
    public TableDataInfo<BpmTaskExtVo> queryPageList(BpmTaskExtBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BpmTaskExt> lqw = buildQueryWrapper(bo);
        Page<BpmTaskExtVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询流程任务扩展列表
     */
    @Override
    public List<BpmTaskExtVo> queryList(BpmTaskExtBo bo) {
        LambdaQueryWrapper<BpmTaskExt> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BpmTaskExt> buildQueryWrapper(BpmTaskExtBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BpmTaskExt> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), BpmTaskExt::getName, bo.getName());
        lqw.eq(bo.getStartTime() != null, BpmTaskExt::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, BpmTaskExt::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessInstanceId()), BpmTaskExt::getProcessInstanceId, bo.getProcessInstanceId());
        lqw.eq(StringUtils.isNotBlank(bo.getExecutionId()), BpmTaskExt::getExecutionId, bo.getExecutionId());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessDefinitionId()), BpmTaskExt::getProcessDefinitionId, bo.getProcessDefinitionId());
        lqw.eq(StringUtils.isNotBlank(bo.getAssignee()), BpmTaskExt::getAssignee, bo.getAssignee());
        lqw.eq(StringUtils.isNotBlank(bo.getAssigneeId()), BpmTaskExt::getAssigneeId, bo.getAssigneeId());
        lqw.like(StringUtils.isNotBlank(bo.getProcessDefinitionName()), BpmTaskExt::getProcessDefinitionName, bo.getProcessDefinitionName());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessDefinitionKey()), BpmTaskExt::getProcessDefinitionKey, bo.getProcessDefinitionKey());
        lqw.eq(StringUtils.isNotBlank(bo.getParentTaskId()), BpmTaskExt::getParentTaskId, bo.getParentTaskId());
        lqw.eq(bo.getFormType() != null, BpmTaskExt::getFormType, bo.getFormType());
        lqw.eq(StringUtils.isNotBlank(bo.getFormOption()), BpmTaskExt::getFormOption, bo.getFormOption());
        lqw.eq(StringUtils.isNotBlank(bo.getFormFields()), BpmTaskExt::getFormFields, bo.getFormFields());
        lqw.eq(StringUtils.isNotBlank(bo.getFormCustomCreatePath()), BpmTaskExt::getFormCustomCreatePath, bo.getFormCustomCreatePath());
        lqw.eq(StringUtils.isNotBlank(bo.getFormCustomViewPath()), BpmTaskExt::getFormCustomViewPath, bo.getFormCustomViewPath());
        lqw.eq(StringUtils.isNotBlank(bo.getFormVariables()), BpmTaskExt::getFormVariables, bo.getFormVariables());
        return lqw;
    }

    /**
     * 新增流程任务扩展
     */
    @Override
    public Boolean insertByBo(BpmTaskExtBo bo) {
        BpmTaskExt add = BeanUtil.toBean(bo, BpmTaskExt.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改流程任务扩展
     */
    @Override
    public Boolean updateByBo(BpmTaskExtBo bo) {
        BpmTaskExt update = BeanUtil.toBean(bo, BpmTaskExt.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BpmTaskExt entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除流程任务扩展
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
